"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const index = require("../virtual-list/index.cjs");
const _hoisted_1 = { class: "me-tree-search-list" };
const _hoisted_2 = ["onClick"];
const _hoisted_3 = ["innerHTML"];
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "tree-list",
  props: {
    list: { default() {
      return [];
    } },
    keyword: { default: "" },
    multiple: { type: Boolean, default: false },
    selectedList: { default() {
      return [];
    } }
  },
  emits: ["click"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const selectedItems = vue.ref([]);
    vue.watchEffect(() => {
      let res = [];
      props.list.forEach((node) => {
        if (props.selectedList.includes(node.node.key)) {
          res.push(node);
        }
      });
      selectedItems.value = JSON.parse(JSON.stringify(res));
    });
    const getSelectedState = (item) => {
      return !!selectedItems.value.filter((node) => node.node.key === item.node.key)[0];
    };
    const getPathContent = (path) => {
      const { keyword } = props;
      let result = [];
      path.forEach((item) => {
        if (item.includes(keyword)) {
          result.push(item.replace(keyword, `<span class="keyword">${keyword}</span>`));
        } else {
          result.push(item);
        }
      });
      return result.join(" / ");
    };
    const findIndex = (key, list) => {
      let index2 = -1;
      list.forEach((item, i) => {
        if (item.node.key === key) {
          index2 = i;
        }
      });
      return index2;
    };
    const onClick = (item) => {
      if (props.multiple) {
        const index2 = findIndex(item.node.key, selectedItems.value);
        if (index2 === -1) {
          selectedItems.value.push(item);
        } else {
          selectedItems.value.splice(index2, 1);
        }
        emits("click", selectedItems.value);
      } else {
        emits("click", item);
      }
    };
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", _hoisted_1, [
        vue.createVNode(vue.unref(index.VirtualList), { "array-list": _ctx.list }, {
          default: vue.withCtx(({ item }) => [
            vue.createElementVNode("div", {
              onClick: ($event) => onClick(item),
              class: vue.normalizeClass(["me-tree-search-list-item", { selected: _ctx.multiple && getSelectedState(item) }])
            }, [
              vue.createElementVNode("span", {
                class: "me-tree-search-list-text",
                innerHTML: getPathContent(item.path)
              }, null, 8, _hoisted_3)
            ], 10, _hoisted_2)
          ]),
          _: 1
        }, 8, ["array-list"])
      ]);
    };
  }
});
exports.default = _sfc_main;
//# sourceMappingURL=tree-list.vue2.cjs.map
